package com.umakr.ax.common.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.push.model.v20160801.PushMessageToAndroidRequest;
import com.aliyuncs.push.model.v20160801.PushMessageToAndroidResponse;
import com.aliyuncs.push.model.v20160801.PushNoticeToAndroidRequest;
import com.aliyuncs.push.model.v20160801.PushNoticeToAndroidResponse;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;

/**
 * @author umakr
 */
public class AliyunPushUtil {
    private AliyunPushUtil() {
        throw new IllegalStateException("AliyunPushUtil class");
    }
    private static final Log logger = LogFactory.getLog(AliyunPushUtil.class);
    static final String ACCESS_KEY_ID = "LTAIWHx1kYTIUmN2";
    static final String ACCESS_KEY_SECRET = "gOrhDX2oACRzBN8BzwoIOIdE9WzuwJ";
    static final Long APP_KEY = Long.parseLong("24782912");
    /**
     * 目前该值固定，不用动
     */
    static final String REGION_ID = "cn-hangzhou";

    /**
     * 从配置文件中读取配置值，初始化Client
     * <p>
     * 1. 如何获取 accessKeyId/accessKeySecret/appKey 照见README.md 中的说明<br/>
     * 2. 先在 push.properties 配置文件中 填入你的获取的值
     * @param userId
     * @param title
     * @param rsId
     * @param rsCode
     * @param rsName
     * @param content
     * @param avatar
     * @throws Exception
     */
    public static void pushMessageToAndroidByAlias(String userId,String title,Integer rsId,String rsCode,String rsName,String content,String avatar) {
        IClientProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultAcsClient client = new DefaultAcsClient(profile);


        PushMessageToAndroidRequest androidRequest = new PushMessageToAndroidRequest();
        //安全性比较高的内容建议使用HTTPS
        androidRequest.setProtocol(ProtocolType.HTTPS);
        //内容较大的请求，使用POST请求
        androidRequest.setMethod(MethodType.POST);
        androidRequest.setAppKey(APP_KEY);
        androidRequest.setTarget("ALIAS");
        androidRequest.setTargetValue(userId);
        androidRequest.setTitle(title);
        String body = "{\"rsId\":"+rsId+",\"rsCode\":\""+rsCode+"\",\"rsName\":\""+rsName+"\",\"content\":\""+content+"\",\"avatar\":\""+avatar+"\",\"type\":\"text\"}" ;
        logger.debug("================="+body);
        androidRequest.setBody(body);
        PushMessageToAndroidResponse pushMessageToAndroidResponse = null;
        try {
            pushMessageToAndroidResponse = client.getAcsResponse(androidRequest);
        } catch (ClientException e) {
            logger.error("context",e);
        }
        System.out.printf("RequestId: %s, MessageId: %s\n", pushMessageToAndroidResponse.getRequestId(), pushMessageToAndroidResponse.getMessageId());

    }
    /**
     * 推送通知给android
     * <p>
     * 参见文档 https://help.aliyun.com/document_detail/48087.html
     * @param userId
     * @param title
     * @param content
     * @throws Exception
     */
    public static void pushNoticeToAndroidByAlias(String userId,String title,String content) throws Exception {
        IClientProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        PushNoticeToAndroidRequest androidRequest = new PushNoticeToAndroidRequest();
        //安全性比较高的内容建议使用HTTPS
        androidRequest.setProtocol(ProtocolType.HTTPS);
        //内容较大的请求，使用POST请求
        androidRequest.setMethod(MethodType.POST);
        androidRequest.setAppKey(APP_KEY);
        androidRequest.setTarget("ALIAS");
        androidRequest.setTargetValue(userId);
        androidRequest.setTitle(title);
        androidRequest.setBody(content);
        PushNoticeToAndroidResponse pushNoticeToAndroidResponse = client.getAcsResponse(androidRequest);
        System.out.printf("RequestId: %s, MessageId: %s\n",
                pushNoticeToAndroidResponse.getRequestId(), pushNoticeToAndroidResponse.getMessageId());

    }

}
